home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Parse.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
4KB
|
226 lines
#include "stdafx.h"
#include <string.h>
#include <ctype.h>
static char val[128], *val_end = val + sizeof(val);
static char lab[128], *lab_end = lab + sizeof(lab);
static int line_no;
static char *data_start, *data_current, *data_end;
cParse::cParse(cParse **list, const char *_label, const char *_value)
{
add_end((cList **)list);
label = strdup(_label);
value = strdup(_value);
line = line_no;
}
cParse::cParse(cParse **list, cParse *orig)
{
add_end((cList **)list);
label = strdup(orig->label);
value = strdup(orig->value);
line = orig->line;
}
cParse::~cParse()
{
delete label;
delete value;
}
int cParse::cmp(char *l)
{
return eq(l, label);
}
int cParse::value_true()
{
return eq(value, "TRUE") || eq(value, "YES");
}
void cParse::invalid_line()
{
error("Invalid line %d: %s = %s", line, label, value);
}
int cParse::pgetc()
{
int c = 0;
for (;;)
{
c = *data_current, data_current++;
if (data_current >= data_end)
return -1;
if (c == '#')
do
{
c = *data_current, data_current++;
if (data_current >= data_end)
return -1;
}
while (c != '\n');
if (c == '\n')
line_no++;
if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
return c;
}
}
int cParse::get_label()
{
char *l = lab;
int c;
for (;;)
{
c = pgetc();
if (c == '=' || c == -1 || l >= lab_end)
{
*l = 0;
return l != lab;
}
else
*l = toupper(c), l++;
}
}
int cParse::get_value()
{
char *v = val;
int c = 0;
c = pgetc();
for (;;)
{
if (c == '\n' || v >= val_end)
return FALSE;
if (c == ';')
{
*v = 0;
return TRUE;
}
else
*v = toupper(c), v++;
c = *data_current, data_current++;
if (data_current >= data_end)
return FALSE;
}
}
void cParse::parse_file(CFile *in, cParse **list)
{
int length = in->GetLength();
// Setup vars
*list = 0;
line_no = 1;
// Read data and setup pointers
data_start = new char[length];
in->Read(data_start, length);
data_current = data_start;
data_end = data_start + length;
// Parse the file in memory
while (get_label())
{
if (!get_value())
error("Parse error in file: %s, line %d.", in->GetFileName(), line_no);
new cParse (list, lab, val);
}
// Remove data from memory
delete data_start;
}
void cParse::parse_file(char *name, cParse **list)
{
TRY
{
CFile f(name, CFile::modeRead | CFile::shareDenyNone);
parse_file(&f, list);
}
CATCH(CFileException, e)
{
error("Unable to open %s", name);
}
END_CATCH
}
char *cParse::get_string(char *name, char *def)
{
for (cParse *p = this; p != 0; p = (cParse *)p->next)
if (p->cmp(name))
return p->value;
return def;
}
int cParse::get_int(char *name, int def)
{
for (cParse *p = this; p != 0; p = (cParse *)p->next)
if (p->cmp(name))
return atoi(p->value);
return def;
}
fix cParse::get_fix(char *name, fix def)
{
for (cParse *p = this; p != 0; p = (cParse *)p->next)
if (p->cmp(name))
return (fix)atof(p->value);
return def;
}
int cParse::get_bool(char *name, int def)
{
for (cParse *p = this; p != 0; p = (cParse *)p->next)
if (p->cmp(name))
return p->value_true();
return def;
}
void cParse::get_spots(char *name, cSpot **list)
{
int x, y;
*list = 0;
for (cParse *p = this; p != 0; p = (cParse *)p->next)
if (p->cmp(name))
{
if(sscanf(p->value, "%d,%d", &x, &y) != 2)
p->invalid_line();
new cSpot(list, x, y);
}
}